package cate.game.farm;

import cate.game.play.proce.play.PlayPO;
import cate.game.role.Role;
import cate.game.role.RoleSnapshot;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

@NoteClass(value = "通关角色数据")
public class PassRole {
    @NoteField(value = "录像唯一id")
    public String replayUid;

    @NoteField(value = "玩家快照")
    public RoleSnapshot owner;

    @NoteField(value = "通关时间")
    public long time;

    @NoteField(value = "通关时间戳")
    public long passTime;

    @NoteField(value = "战力")
    public long power;

    public void initialize() {
        if (time <= 1000L) {
            replayUid = null;
        }
    }

    /**
     * 如果成功记录，返回老的录像id
     * @param role 角色
     * @param playPO 录像
     * @param power 战力
     * @return 录像id
     */
    public String record(Role role, PlayPO playPO, long power) {
        if (StringUtils.isBlank(replayUid) || time > playPO.endTime - playPO.startTime) {
            String oldReplayUid = replayUid;
            update(role, playPO, power);
            return oldReplayUid;
        }
        return null;
    }

    private void update(Role role, PlayPO playPO, long power) {
        this.owner = new RoleSnapshot().read(role);
        this.time = playPO.endTime - playPO.startTime;
        this.power = power;
        this.replayUid = playPO.uid;
        this.passTime = System.currentTimeMillis();
    }
}
